package com.ruicheng.iam.service.impl;

import com.ruicheng.iam.common.IdWorker;
import com.ruicheng.iam.dao.ProjectInfoDao;
import com.ruicheng.iam.dto.ProjectInfoDTO;
import com.ruicheng.iam.dto.ProjectSearchDTO;
import com.ruicheng.iam.entity.ProjectInfo;
import com.ruicheng.iam.service.IProjectInfoService;
import com.ruicheng.iam.util.RestResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 项目信息业务层实现
 * @author huang guang yue
 * @version v1.0.0
 * @date 2020 2020/11/24 16:54
 */
@Service
public class ProjectInfoServiceImpl implements IProjectInfoService {

    @Resource
    private ProjectInfoDao projectInfoDao;

    @Override
    public List<ProjectInfo> listByAppCode(String appCode) {
        return projectInfoDao.listByAppCode(appCode);
    }

    @Override
    public RestResult<Object> insertSelective(ProjectInfoDTO dto) {

        List<ProjectInfo> projectInfos = projectInfoDao.listByAppCode(dto.getAppCode());
        if (projectInfos != null && projectInfos.size() > 0){
            return RestResult.error("项目重复");
        }

        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(dto, projectInfo);
        Date date = new Date();
        projectInfo.setCreateTime(date);
        projectInfo.setUpdateTime(date);
        projectInfo.setId(IdWorker.nextId());
        int i = projectInfoDao.insertSelective(projectInfo);
        if (i == 1){
            return RestResult.success();
        } else {
            return RestResult.error();
        }
    }

    @Override
    public RestResult<List<ProjectInfo>> searchProject(ProjectSearchDTO dto) {
        return RestResult.success(projectInfoDao.searchProject(dto));
    }
}
